<template>
	<view class="add_box">
		<view class="add_item">
			<view>类型</view>
			<picker mode="selector" :range="typesList" @change="getPickerValue" class="add_picker" range-key="title" :data-index="0">
				<text>{{typesList[typesIndex].title}}</text>
				<image src="/static/images/right.png" class="right"></image>
			</picker>
		</view>
		<view class="add_item">
			<view>客户选择</view>
			<picker mode="selector" :range="clientList" @change="getPickerValue" class="add_picker" range-key="title" :data-index="1">
				<text>{{clientList[clientIndex].title}}</text>
				<image src="/static/images/right.png" class="right"></image>
			</picker>
		</view>
		<view class="add_user">
			<view class="add_user_title">审批人</view>
			<view class="add_user_box">
				<view class="add_user_item">
					<view class="add_user_item_img" @click.stop="openSearch('all_user4')">
								<!-- <image></image> --> 
						<view>{{approvalName == '' ? "+" : approvalName}}</view>
					</view>
				</view>
			</view>
		</view>
		<view class="add_user">
			<view class="add_user_title">审核人</view>
			<view class="add_user_box">
				<view class="add_user_item">
					<view class="add_user_item_img" @click.stop="openSearch('all_user5')">
						<view>{{auditName== '' ? "+" : auditName}}</view>
					</view>
				</view>
			</view>
		</view>
		<!-- <view class="add_user">
			<view class="add_user_title">抄送人</view>
			<scroll-view scroll-x="true" class="add_user_scroll">
				<view class="add_user_box">
					<block v-for="(item,index) in copyList">
						<view class="add_user_item" :key="index">
							<image src="/static/images/user_radius.png" class="add_user_item_icon" v-if="index > 0"></image>
							<view class="add_user_item_img">
								<view>{{item.username}}</view>
								<image src="/static/images/deletet.png" @tap="deleteCopy(index)"></image>
							</view>
						</view>
					</block>
					<picker mode="selector" :range="userList" @change="getPickerValue" range-key="username" :data-index="3">
						<view class="add_user_item">
							<image src="/static/images/user_radius.png" class="add_user_item_icon" v-if="copyList.length > 0"></image>
							<view class="add_user_item_img">
								<view>+</view>
							</view>
						</view>
					</picker>
				</view>
			</scroll-view>
		</view> -->
		<!-- <view class="add_item">
			<view>标题</view>
			<input type="text" v-model="upJson.title" placeholder="请输入标题"/>
		</view> -->
		<!-- <view class="add_item">
			<view>相关客户</view>
			<picker mode="selector" :range="clientList" @change="getPickerValue" class="add_picker" range-key="title" :data-index="4">
				<text>{{upJson.relate_customer}}</text>
				<image src="/static/images/right.png" class="right"></image>
			</picker>
		</view> -->
		<view class="add_item">
			<view>关联订单</view>
			<picker mode="selector" :range="orderList" @change="getPickerValue" class="add_picker" range-key="order_no" :data-index="5" :disabled="orderIndex == -1 ? true : false">
				<text>{{orderIndex == -1 ? '暂无相关订单' : orderList[orderIndex].order_no}}</text>
				<image src="/static/images/right.png" class="right"></image>
			</picker>
		</view>
		<view class="add_icon">
			<view>销售产品</view> 
			<view>
				<image src="/static/images/code.png" mode=""></image>
				<picker mode="selector" :range="productList" @change="getPickerValue" class="add_icon_picker" range-key="product_name" :data-index="6" :disabled="orderIndex == -1 ? true : false">
					<view><image src="/static/images/add.png" mode=""></image></view>
				</picker>
			</view>
		</view>
		
		<block v-for="(item,index) in upJson.product_list">
			<view class="add_item" :key="index">
				<view>产品名称</view>
				<view class="add_picker">
					<text>{{item.name}}</text>
					<image src="/static/images/visit_icon4.png" class="delete" @tap="delProduct(index)"></image>
				</view>
			</view>
			<view class="add_item">
				<view>数量</view>
				<input type="text" v-model="upJson.product_list[index].number" placeholder="请输入数量" @blur="mathPrice()"/>
			</view>
			<view class="add_item">
				<view>销售单价(元)</view>
				<input type="text" v-model="upJson.product_list[index].price" placeholder="请输入销售单价" @blur="mathPrice()"/>
			</view>
			<view class="add_item">
				<view>库房</view>
				<picker mode="selector" :range="houseList" @change="getPickerValue" :range-key="'title'" class="add_picker" :data-index="7" :data-indexs="index">
					<text>{{item.warehouse_name}}</text>
					<image src="/static/images/right.png" class="right"></image>
				</picker>
			</view>
		</block>
		<view class="add_item" v-if="typesIndex==0">
			<view>退款总金额</view>
			<input type="text" v-model="upJson.relate_price" placeholder="请输入退款总金额"/>
		</view>
		<view class="add_item">
			<view>退回库房</view>
			<picker mode="selector" :range="backHouseList" @change="getPickerValue" class="add_picker" range-key="title" :data-index="8">
				<text>{{backHouseList[backHouseIndex].title}}</text>
				<image src="/static/images/right.png" class="right"></image>
			</picker>
		</view>
		<view class="add_item" v-if="typesIndex==1">
			<view>换货仓库</view>
			<picker mode="selector" :range="houseList" @change="getPickerValue" class="add_picker" range-key="title" :data-index="9">
				<text>{{houseList[houseIndex].title}}</text>
				<image src="/static/images/right.png" class="right"></image>
			</picker>
		</view>
		<view class="add_item" v-if="typesIndex==1">
			<view>换货产品</view>
			<picker mode="selector" :range="allProductList" @change="getPickerValue" class="add_picker" range-key="title" :data-index="10">
				<text>{{allProductList[allProductIndex].title}}</text>
				<image src="/static/images/right.png" class="right"></image>
			</picker>
		</view>
		<!-- <view class="add_item" v-if="typesIndex==1">
			<view>产品数量</view>
			<input type="text" v-model="upJson.relate_price" placeholder="请输入换货产品数量"/>
		</view> -->
		
		
		<view class="add_textarea">
			<view>审批内容</view>
			<textarea  v-model="upJson.content" placeholder="请输入审批内容" />
		</view>
		
		<view class="save" @tap="submit()">发起审批</view>
	</view>
</template>

<script>
	import {
		deepClone
	} from '@/static/js/index.js'
	export default{
		data(){
			return{
				typesList:[],
				typesIndex:0,
				clientList:[],
				userList:[],
				orderList:[],
				productList:[],
				allProductList:[],
				copyList:[],
				houseList:[],
				backHouseList:[],
				houseIndex:0,
				backHouseIndex:0,
				clientIndex:0,
				clientIndex1:0,
				orderIndex:-1,
				approceIndex:-1,
				productIndex:0,
				allProductIndex:0,
				auditName:'', // 审核人
				approvalName:'', // 审批人
				upJson:{
					type:0,//类型
					customer_id:"",//客户id
					order_id:"",//订单ID
					order_no:"", // 订单编号
					// title:"",//审批标题
					content:"",//审批内容
					audit_id:"",// 审核人
					approval_id:"", // 审批人
					cc_id:"",//抄送人
					user_id:uni.getStorageSync("userId"),//发起人ID
					// relate_customer:"",//关联客户
					relate_price:"",//退款金额
					product_list:[],//退货列表
					insert_product:[], // 换货列表
					//order_price:0 // 新增总价
				}
			} 
		},
		onLoad(options) {
			{//暂时===
				this.typesList=[{id:0,title:'退货'},{id:1,title:'换货'}];
				this.upJson.type=this.typesList[0].id;
			}
			this.$api.getClientList({class:1,belong:1,user_id:uni.getStorageSync("userId")}).then(res =>{
				this.clientList = res.data;
				this.upJson.customer_id = res.data[0].id;
				// this.upJson.relate_customer = res.data[0].title;
				this.getRelationOrderList(this.upJson.customer_id);
			})
			this.$api.getUserList({}).then(res =>{
				let list = [];
				for(let i in res){
					for(let j in res[i].user_list){
						list.push(res[i].user_list[j])
					}
				}
				this.userList = list;
			})
			this.$api.getProductLibrary({}).then(res => {
				this.houseList = res;
				this.backHouseList = res;
				this.getProductList(); // 加载产品数据
			})
		},
		onShow(){
			this.setBaseNews();
		},
		methods:{
			setBaseNews(){//设置基础信息
				let obj=deepClone(this.$store.getters['parameters/searchObj']);
				//console.log("--返回值-->>>>>>",obj);
				if(obj.key=="all_user4"){

					this.approvalName = obj.data.username;
					this.upJson.approval_id = obj.data.id;
				}else if(obj.key=="all_user5"){
					this.auditName = obj.data.username;
					this.upJson.audit_id = obj.data.id;
				}
			},
			//打开搜索页面
			openSearch(key){

					uni.navigateTo({
						url:`/pages/searchList/searchList?searchKey=`+key
					})

			},
			// 获取产品
			getProductList(){
				let warehouse_id = this.houseList[this.houseIndex].id;
				let upJson = {page:1,warehouse_id:warehouse_id}

				this.$api.getProductList(upJson).then(res => {
					this.allProductList = res.data;					
 				})
			},
			// 获取客户关联订单数据
			getRelationOrderList(customer_id){
				this.$api.getRelationOrderList({customer_id:customer_id}).then(res =>{
					// this.upJson.product_list=res.;
					//console.log('res==-----数量===='+res.length,res)
					this.orderList = res
					if(res.length > 0){
						this.orderIndex = 0;
						this.upJson.order_id = res[0].id
						this.upJson.order_no = res[0].order_no
						this.productList = res[0].product_list;
					}else{
						this.orderIndex = -1
					}
				})
			},
			delProduct(index){//删除产品
				this.upJson.product_list.splice(index,1);
				this.mathPrice();
			},
			deleteCopy(index){
				this.copyList.splice(index,1);
			},
			getPickerValue(e){
				console.log(e)
				if(e.currentTarget.dataset.index == 0){//类型
					this.typesIndex = e.detail.value;
					this.upJson.type = this.typesList[this.typesIndex].id;
				}else if(e.currentTarget.dataset.index == 1){//客户

					this.clientIndex = e.detail.value;
					this.customer_id = this.clientList[this.clientIndex].id;
					this.getRelationOrderList(this.customer_id);

				}else if(e.currentTarget.dataset.index == 2){//审批人
					this.approceIndex = e.detail.value;
					this.upJson.audit_id = this.userList[this.approceIndex].id
				}else if(e.currentTarget.dataset.index == 3){//抄送人
					for(let i in this.copyList){
						if(this.userList[e.detail.value].username == this.copyList[i].username){
							uni.showToast({title:"抄送人不能重复",icon:"none"})
							return;
						}
					}
					this.copyList.push(this.userList[e.detail.value])
				}else if(e.currentTarget.dataset.index == 4){//相关客户
					this.upJson.relate_customer = this.clientList[e.detail.value].title;
					
				}else if(e.currentTarget.dataset.index == 5){//相关订单
					this.orderIndex = e.detail.value;
					this.upJson.order_id = this.orderList[this.orderIndex].id
					this.productList = this.orderList[this.orderIndex].product_list
				}else if(e.currentTarget.dataset.index == 6){//销售产品
					this.upJson.product_list.push({product_id:this.productList[e.detail.value].product_id,name:this.productList[e.detail.value].product_name,number:this.productList[e.detail.value].number,warehouse_id:this.houseList[0].id,warehouse_name:this.houseList[0].title,price:this.productList[e.detail.value].price})
					this.mathPrice();
				}else if(e.currentTarget.dataset.index == 7){//库房
					this.upJson.product_list[Number(e.currentTarget.dataset.indexs)].warehouse_id = this.houseList[e.detail.value].id
					this.upJson.product_list[Number(e.currentTarget.dataset.indexs)].warehouse_name = this.houseList[e.detail.value].title
				}else if(e.currentTarget.dataset.index == 8){ // 退还仓库
					this.backHouseIndex = e.detail.value;
				}else if(e.currentTarget.dataset.index == 9){ // 换货仓库
					this.houseIndex = e.detail.value;
					this.getProductList();
				}else if(e.currentTarget.dataset.index == 10){ // 换货产品
					this.allProductIndex = e.detail.value;
					this.upJson.insert_product[0] = this.allProductList[e.detail.value];
				}
				console.log('-----库房id----》》》》',this.houseIndex);
			},
			mathPrice(){//计算价格
				let relate_price = 0;
				for(let i in this.upJson.product_list){
					relate_price += Number(this.upJson.product_list[i].price) * Number(this.upJson.product_list[i].number);
				}
				this.upJson.relate_price = relate_price
			},
			submit(){
				if(this.upJson.approval_id == '' || this.upJson.approval_id.length == 0){
					uni.showToast({title:"审批人不能为空",icon:"none"})
				}else if(this.upJson.audit_id == '' || this.upJson.audit_id.length==0){
					uni.showToast({title:"审核人不能为空",icon:"none"})
				}else if(Number(this.upJson.relate_price) == 0){
					uni.showToast({title:"退款总金额不能小于0",icon:"none"})
				}else if(this.upJson.product_list.length == 0){
					uni.showToast({title:"退货产品不能为空",icon:"none"})
				}else{
					this.upJson.cc_id = "";
					for(let i in this.upJson.product_list){
						if(Number(this.upJson.product_list[i].number) <= 0){
							uni.showToast({title:"产品数量需要大于0",icon:"none"})
							return
						} 
						this.upJson.product_list[i].number = Number(this.upJson.product_list[i].number);
					}
					let upJson = {};
					for(let key in this.upJson){
						upJson[key] = this.upJson[key]
					}
					upJson.product_list = JSON.stringify(upJson.product_list);
					for(let i in this.copyList){
						i == 0 ? this.upJson.cc_id += this.copyList[i].id : this.upJson.cc_id +=  "," + this.copyList[i].id
					} 
					this.$api.addReturn(upJson).then(res =>{
						this.$app.globalData.upData.return = true
					})
				}
			}
		}
	}
</script>

<style>
</style>
